
import java.util.Arrays;
import java.util.Scanner;

public class ScoreInquiry {
    public static void main(String[] args) { //静态方法只能调用静态方法
       String  select ;
       String command ;
       String []str_Arrays ;
       String [] student_Names = new String[]{ "zhangsan" , "lisi" , "wangwu" , "zhaoliu" , "qianqi" };
       String [] courses = new String[]{ "corejava" , "JDBC" , "javaWeb" , "EJB" } ;
       double [][] scores = new double[][]{
               {87 , 98 , 65 , 77},
               {69 , 75 , 68 , 91},
               {81 , 70 , 69 , 72},
               {90 , 88 , 82 , 76},
               {92 , 74 , 51 , 82}
       };
       Scanner scanner = new Scanner( System.in ) ;
        while (true){                                                  // 命令匹配函数
            System.out.println("请输入命令：");
            command = scanner.nextLine() ;
            str_Arrays = command.split(" ") ;
            select = str_Arrays[0] ;
             switch (select) {
                 case "avg":
                     System.out.println("正在执行操作命令...");
                     average( str_Arrays[1] , student_Names , courses , scores );
                     break;
                 case "sort":
                     sort( str_Arrays[1] , student_Names , courses , scores );
                     break;
                 case "sum":
                     sum( str_Arrays[1] , student_Names , courses , scores );
                     break;
                 case "get":
                     get( str_Arrays , student_Names , courses , scores );
                     break;
                 case "exit":
                     return;
                 default:
                     System.out.println("错误命令");
             }

       }

    }


    public static void average ( String object ,String []student_Names ,String []courses ,double [][]scores ){
        double sum = 0;
        double avg;

        for (int i = 0; i < student_Names.length; i++) {   //判断需要查询的是否为具体学生的平均成绩
            if ( object.equals(student_Names[i]) ){               //如果找到对应学生姓名则输出对应平均成绩
                for (int j = 0; j < courses.length; j++) {
                    sum += scores[i][j] ;
                }
                System.out.println( sum );
                avg = sum / courses.length ;
                System.out.println( object + "的平均分为：" + avg );
                return;
            }
        }
        for (int i = 0; i < courses.length; i++) {         //判断需要查询的是否为具体课程的平均成绩
            if ( object.equals(courses[i]) ){                     //如果找到对应课程名则输出对应平均成绩
                for (int j = 0; j < student_Names.length; j++) {
                    sum += scores[j][i] ;
                }
                System.out.println( sum );
                avg = sum / student_Names.length ;
                System.out.println( object + "的平均分为：" + avg );
                return;
            }
        }

    }

    public static void sort ( String object ,String []student_Names ,String []courses ,double [][]scores ){
        double []sum1 = new double[student_Names.length];
        if ( object.equals( "sum") ){
            for (int i = 0; i < student_Names.length; i++) {
                    for (int j = 0; j < courses.length; j++) {
                        sum1[i] += scores[i][j] ;
                    }
            }
            Arrays.sort(sum1);   //排序总分，但是未交换对应姓名。
            for (int i = 0; i < sum1.length; i++) {
                System.out.print(student_Names[i] + ":" + sum1[i] + "  ");
            }
            System.out.println();
            return;
        }
        for (int i = 0; i < courses.length; i++) {
            if ( object.equals(courses[i]) ){
                for (int j = 0; j < student_Names.length; j++) {
                    sum1[j] = scores[j][i] ;
                }
                Arrays.sort(sum1);
                System.out.print(courses[i] + ":" );
                for (int j = 0; j < sum1.length; j++) {
                    System.out.print("\t" + sum1[j] + "  ");
                }
                System.out.println();
                return;
            }
        }

    }
    public static void sum (String object ,String []student_Names ,String []courses ,double [][]scores ){
        int sum = 0 ;
        for (int i = 0; i < student_Names.length; i++) {
            if ( object.equals(student_Names[i]) ){
                for (int j = 0; j < courses.length; j++) {
                    sum += scores[i][j] ;
                }

                System.out.println(student_Names[i] + "总分为 :" + sum );
                return;
            }
        }

    }
    public static void get ( String[] object ,String []student_Names ,String []courses ,double [][]scores ){
        for (int i = 0; i < student_Names.length; i++) {
            if ( object[1].equals(student_Names[i]) ){
                for (int j = 0; j < courses.length; j++) {
                    if ( object[2].equals(courses[j]) ){
                        System.out.println(student_Names[i] + "的" +  courses[j] + "成绩为：" + scores[i][j] );
                        return;
                    }
                }

            }
        }
        return;
    }

}
